package cn.cmkj.auction.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.xutils.image.ImageOptions;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Address;
import cn.cmkj.auction.ui.UserAddressActivity;
import cn.cmkj.auction.ui.UserAddressEditActivity;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class AddressOfListAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<Address> mData;
    private ImageOptions imageOptions;
    private int fromPage;

    public AddressOfListAdapter(Context context, List<Address> mData, int fromPage) {
        this.context = context;
        this.mData = mData;
        this.fromPage = fromPage;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int getItemViewType(int position) {
        Address address = getItem(position);
        if(address != null) {
            if(address.isDefault()) {
                return 0;
            }
        }
        return 1;
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    public Address getItem(int position) {
        if(mData == null || mData.size() <= 0)return null;
        Address item = mData.get(position);
        return item;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_list_address, parent, false);
        return new AddressViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Address item = mData.get(position);
        if(item != null) {
            ((AddressViewHolder) holder).addressName.setText(item.getName());
            ((AddressViewHolder) holder).addressPhone.setText(item.getMobile());
            ((AddressViewHolder) holder).addressLocation.setText(item.getAddr());
            ((AddressViewHolder) holder).rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(fromPage == UserAddressActivity.FROM_MINE) {
                        Intent intent = new Intent(context, UserAddressEditActivity.class);
                        intent.putExtra("address", item);
                        context.startActivity(intent);
                    }else if(fromPage == UserAddressActivity.FROM_ORDER) {
                        Intent intent = new Intent();
                        intent.putExtra("address", item);
                        ((Activity)context).setResult(Activity.RESULT_OK, intent);
                        ((Activity)context).finish();
                    }
                }
            });
        }
    }

    class AddressViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout rootView;
        public TextView addressName;
        public TextView addressPhone;
        public TextView addressLocation;

        public AddressViewHolder(View itemView) {
            super(itemView);
            this.rootView = (LinearLayout) itemView.findViewById(R.id.linear_layout);
            this.addressName = (TextView) itemView.findViewById (R.id.address_name);
            this.addressPhone = (TextView) itemView.findViewById (R.id.address_phone);
            this.addressLocation = (TextView) itemView.findViewById (R.id.address_location);
        }
    }
}